//
//  NSMutableAttributedString+AttributedStringConfig.m
//  MyFunctionDemo
//
//  Created by Ting Han on 2017/9/1.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "NSMutableAttributedString+AttributedStringConfig.h"

@implementation NSMutableAttributedString (AttributedStringConfig)

+ (instancetype)mutableAttributedStringWithString:(NSString *)string config:(void (^)(NSString *, NSMutableArray<AttributedStringConfig *> *))configBlock{
    NSMutableAttributedString *atbString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableArray            *array     = nil;
    
    if (configBlock) {
        
        array = [NSMutableArray array];
        configBlock(string, array);
    }
    
    [array enumerateObjectsUsingBlock:^(AttributedStringConfig *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [atbString addStringAttribute:obj];
    }];
    
    return atbString;
}
- (void)addStringAttribute:(AttributedStringConfig *)stringAttribute{
    [self addAttribute:stringAttribute.attributeName value:stringAttribute.attributeValue range:stringAttribute.effectiveStringRange];
}
- (void)removeStringAttribute:(AttributedStringConfig *)stringAttribute{
    [self removeAttribute:stringAttribute.attributeName range:stringAttribute.effectiveStringRange];
}

@end
